<?php

namespace App\Http\Controllers\admin;

use App\model\UserModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;

class LoginController extends Controller
{
    public function login(Request $request)
    {
        if ($request->isMethod('POST')) {
            $this->validate($request, [
                'name' => 'required',
                'password' => 'required'
            ], [
                'required' => ':attribute 为必填项',
            ]);
            if (empty(UserModel::find('name'))) {
               return redirect('admin/register')->with('msg', '输入用户名不存在，请先注册');
            } else if ($request->name != UserModel::find('name') && $request->password != UserModel::find('password')) {
               return redirect('admin/login')->with('msg', '输入用户或者密码错误，请重新填写');
            } else {
                Session::put('name', $request->name);
                Session::put('password', $request->password);
                return redirect('index')->with('success','登录成功');
            }
        }
        return view('AdmibViews.login');
    }
}
